<?php

namespace app\admin\controller;

use app\admin\model\Fake as ModelFake;
use app\common\LogService;

class Fake extends Base
{
    public function index()
    {
        return view();
    }
    public function add()
    {
        return view();
    }
    public function doAdd()
    {
        $type = input('type');
        $value = input('value');
        $count = input('count');
        $res = (new ModelFake())->generateCards($type, $value, $count);
        if ($res['code'] == 200) return success();
        return error($res['msg']);
    }
    public function list()
    {
        $limit = input('limit');
        $fake = input('fake');
        $status = input('status');
        $type = input('type');
        $list = ModelFake::order('id desc,status asc');
        if (!empty($type)) {
            $list->where(['type' => $type]);
        }
        if (!empty($fake)) {
            $list->where('fake', 'like', "%$fake%");
        }
        if (isset($status) && ($status == 0 || $status == 1)) {
            $list->where(['status' => $status]);
        }
        $list = $list->paginate($limit);
        return success($list);
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '批量删除卡密',
                target_id: $id,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $ids = explode(',', $id);
        $res = ModelFake::destroy($ids);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
}
